package com.lqian.demo01;

import com.lqian.utils.SleepHelper;

import java.util.concurrent.locks.ReentrantLock;

public class Interrupt_and_Lock {
    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            lock.lock();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
            System.out.println("t1 end。。");
        });
        t1.start();
        SleepHelper.sleepSeconds(1);

        Thread t2 = new Thread(()->{
            lock.lock(); //  t2.interrupt(); 会不会干扰到正在竞争锁资源的t2呢？
             // 不会
            //  synchronized和lock。如果说有一个线程，它持有了一把锁，它老是不释放。
            //  另外一把锁去申请这把锁的中间过程 用interrupt是干扰不到的
            // 如果想干扰怎么办呢？  >>> lockInterruptibly
            try {
            } finally {
                lock.unlock();
            }
            System.out.println("t2 end..");
        });
        t2.start();
        SleepHelper.sleepSeconds(1);
        t2.interrupt();
    }
}
